import { LoginParams } from '@/types/request'
import { LoginResponse } from '@/types/response'
import { MockMethod } from 'vite-plugin-mock'
import { responseError, responseSuccess } from './_util'

export default [
  {
    url: '/api/login',
    method: 'post',
    response ({ body }: { body: LoginParams}) {
      if (body.username === 'admin') {
        return responseSuccess<LoginResponse>({
          token: 'admin-token'
        }, '登录成功')
      }
      return responseError({}, '用户名或密码错误')
    }
  },
  {
    url: '/api/logout',
    method: 'get',
    response ({ headers }) {
      const user = headers.authorization.match(/^Bearer (.*)-token$/)[1]
      return responseSuccess({}, `${user} 登出成功`)
    }
  }
] as MockMethod[]
